<?php 


/** 
***************************************************
*欢迎使用免费版xSite企业自助建站软件！
*这是一个专业为搜索引擎营销而设计的，极稳定、极易维护的建站软件！
*官方客服：help@php.net.cn
*官方下载网址：http://www.php.net.cn/xsite/ 
***************************************************
**/




 class db_mysql { var $exit = false; var $debug = true; var $msg = array(); var $sql = ""; var $link; var $query; var $insert_id; var $total_row = 0; var $is_mysqli = false; var $query_num = 0; function db_mysql($dbhost = null, $dbuser = null, $dbpass = null, $dbname = null, $dbpconnect = null) { if (class_exists('mysqli')) { $this->is_mysqli = true; } else { $this->is_mysqli = false; } if ($dbhost == null || $dbuser == null || $dbpass == null && $dbname == null && $dbpconnect == null) { $db = &$GLOBALS['_Db_Conf']; } if (!$dbpconnect) { if ($this->is_mysqli) { $this->link = mysqli_connect($db['host'], $db['user'], $db['password'], $db['dbname']); } else { if (!($this->link = mysql_connect($db['host'], $db['user'], $db['password']))) { $this->halt("数据操链接失败！"); } } }else { if ($this->is_mysqli) { $this->link = mysqli_connect($db['host'], $db['user'], $db['password'], $db['dbname']); } else { if (!($this->link = mysql_pconnect($db['host'], $db['user'], $db['password']))) { $this->halt("数据操链接失败！"); } } } if($this->version() > '4.1') { if (in_array(strtolower(DB_CHARSET), array('gbk', 'gb2312', 'big5', 'utf8'))) { $sql = "SET character_set_connection=" . DB_CHARSET . ", character_set_results=" . DB_CHARSET . ", character_set_client=binary"; $this->is_mysqli? mysqli_query($this->link, $sql) : mysql_query($sql, $this->link); } if($this->version() > '5.0.1') { $this->is_mysqli? mysqli_query($this->link, "SET sql_mode=''") : mysql_query("SET sql_mode=''", $this->link); } $this->is_mysqli? mysqli_query($this->link, 'set names ' . DB_CHARSET ) : mysql_query('set names ' . DB_CHARSET, $this->link); } if ($this->is_mysqli) { if (!mysqli_select_db($this->link, $db['dbname'])) { $this->halt("你选择的数据不存在！"); } } else { if (!mysql_select_db($db['dbname'], $this->link)) { $this->halt("你选择的数据不存在！"); } } } function version() { return $this->is_mysqli ? mysqli_get_server_info($this->link) : mysql_get_server_info($this->link); } function query($sql) { $this->sql = $sql; if ($this->is_mysqli) { if ($this->query = mysqli_query($this->link, $sql)) { if (preg_match('/select/is', $this->sql)) { $this->total_row = mysqli_num_rows($this->query); } $this->insert_id = mysqli_insert_id($this->link); return true; } else { if (mysqli_errno($this->link) == 2006) { $this->dbClose(); $this->db_mysql(); } if (!mysqli_query($this->link, $sql)) { $this->halt("数据操作失败，请检查\n[ " . $sql . " ]\n语法是否正确！Error:" . mysqli_error($this->link)); return false; } else { $this->insert_id = @mysqli_insert_id($this->link); return true; } } } else { if ($this->query = mysql_query($sql, $this->link)) { $this->total_row = @mysql_num_rows($this->query); $this->insert_id = @mysql_insert_id($this->link); return true; } else { if (mysql_errno($this->link) == 2006) { $this->dbClose(); $this->db_mysql(); } if (!mysql_query($sql, $this->link)) { $this->halt("数据操作失败，请检查\n[ " . $sql . " ]\n语法是否正确！Error:" . mysql_error($this->link)); return false; } else { $this->insert_id = @mysql_insert_id($this->link); return true; } } } } function fetchRow($num = 0) { if ($this->is_mysqli) { if ($this->total_row) { if (mysqli_data_seek($this->query, $num)) { return mysqli_fetch_array($this->query, MYSQL_ASSOC); }else { return array(); } } else { return array(); } } else { if ($this->total_row) { if (mysql_data_seek($this->query, $num)) { return mysql_fetch_array($this->query, MYSQL_ASSOC); }else { return array(); } } else { return array(); } } } function fetchAll($field = null) { $temp = array(); if ($this->total_row) { if ($field == null) { for($i = 0; $i < $this->total_row; $i++) { $this->is_mysqli ? mysqli_data_seek($this->query, $i) : mysql_data_seek($this->query, $i); $temp[] = $this->is_mysqli ? mysqli_fetch_array($this->query, MYSQL_ASSOC) : mysql_fetch_array($this->query, MYSQL_ASSOC); } } else { $field = trim($field); $field = explode(",", $field); if (is_array($field) && count($field)) { for($i = 0; $i<$this->total_row; $i++) { $this->is_mysqli ? mysqli_data_seek($this->query, $i) : mysql_data_seek($this->query, $i); $result = $this->is_mysqli ? mysqli_fetch_array($this->query, MYSQL_ASSOC) : mysql_fetch_array($this->query, MYSQL_ASSOC); foreach ($field as $key => $value) { $temp[$i][$value] = $result[$value]; } return $temp; } } else { return array(); } } return $temp; } else { return array(); } } function delete($tablename, $where) { if (empty($tablename))return false; if (empty($where))return false; $this->sql = $sql = "DELETE FROM `" . $tablename . "` WHERE " . $where; if ($this->is_mysqli) { if (mysqli_query($this->link, $sql)) { return true; } else { if (mysqli_errno($this->link) == 2006) { $this->dbClose(); $this->db_mysql(); } if (!mysqli_query($sql, $this->link)) { $this->halt("数据操作失败，请检查\n[ " . $sql . " ]\n语法是否正确！Error:" . mysqli_error($this->link)); return false; } else { return true; } } } else { if (mysql_query($sql, $this->link)) { return true; } else { if (mysql_errno($this->link) == 2006) { $this->dbClose(); $this->db_mysql(); } if (!mysql_query($sql, $this->link)) { $this->halt("数据操作失败，请检查\n[ " . $sql . " ]\n语法是否正确！Error:" . mysql_error($this->link)); return false; } else { return true; } } } } function insert($tablename, $array) { if (empty($tablename))return false; if (!is_array($array))return false; while (list($key, $value) = each($array)) { $temp[] = '`' . $key . "` = '" . $value . "'"; } $this->sql = $sql = "INSERT INTO `" . $tablename . "` SET " . implode(", ", $temp); if ($this->is_mysqli) { if (mysqli_query($this->link, $sql)) { $this->insert_id = mysqli_insert_id($this->link); return true; } else { if (mysqli_errno($this->link) == 2006) { $this->dbClose(); $this->db_mysql(); } if (!mysqli_query($this->link, $sql)) { $this->halt("数据操作失败，请检查\n[ " . $sql . " ]\n语法是否正确！Error:" . mysqli_error($this->link)); return false; } else { $this->insert_id = mysql_insert_id($this->link); return true; } } } else { if (mysql_query($sql, $this->link)) { $this->insert_id = mysql_insert_id($this->link); return true; } else { if (mysql_errno($this->link) == 2006) { $this->dbClose(); $this->db_mysql(); } if (!mysql_query($sql, $this->link)) { $this->halt("数据操作失败，请检查\n[ " . $sql . " ]\n语法是否正确！Error:" . mysql_error($this->link)); return false; } else { $this->insert_id = mysql_insert_id($this->link); return true; } } } } function update($tablename, $set, $where) { if (empty($tablename))return false; if (!is_array($set))return false; if (empty($where))return false; while (list($key, $value) = each($set)) { $temp[] = '`' . $key . "` = '" . $value . "'"; } $this->sql = $sql = "UPDATE `" . $tablename . "` SET " . implode(", ", $temp) . " WHERE " . $where; if ($this->is_mysqli) { if (mysqli_query($this->link, $sql)) { return true; } else { if (mysqli_errno($this->link) == 2006) { $this->dbClose(); $this->db_mysql(); } if (!mysqli_query($this->link, $sql)) { $this->halt("数据操作失败，请检查\n[ " . $sql . " ]\n语法是否正确！Error:" . mysqli_error($this->link)); return false; } else { return true; } } } else { if (mysql_query($sql, $this->link)) { return true; } else { if (mysql_errno($this->link) == 2006) { $this->dbClose(); $this->db_mysql(); } if (!mysql_query($sql, $this->link)) { $this->halt("数据操作失败，请检查\n[ " . $sql . " ]\n语法是否正确！Error:" . mysql_error($this->link)); return false; } else { return true; } } } } function dbClose() { if ($this->is_mysqli) { mysqli_close($this->link); } else { mysql_close($this->link); } } function halt($message) { $this->msg[] = $message; $err = ''; if($this->debug){ if(count($this->msg)){ foreach ($this->msg as $k=>$v){ $err .= "\n".($k+1)." : ".$v."\n"; } } echo $err; if($this->exit){ exit(); } else { return true; } }else { return false; } } } function db_multipage($total_row, $page = 1, $limit = 30) { if ($total_row < 1) { $total_row = 1; } $total_page = ceil($total_row / $limit); $current_page = $page; $current_page = ($current_page < 1) ? 1 : $current_page; $current_page = ($current_page > $total_page) ? $total_page : $current_page; $start_row = ($current_page - 1) * $limit; $up_page = $current_page - 1; $up_page = ($up_page < 1) ? 1 : $up_page; $next_page = $current_page + 1; $next_page = ($next_page > $total_page) ? $total_page : $next_page; $offset = 5; if ($current_page <= $offset) { for ($i = 1; $i < $current_page; $i++) { $db_multipage['link'][] = $i; } } else { for ($i = $current_page - $offset; $i < $current_page; $i++) { $db_multipage['link'][] = $i; } } $db_multipage['link'][] = $current_page; if ($current_page + $offset > $total_page) { for ($i = $current_page + 1; $i <= $total_page; $i++) { $db_multipage['link'][] = $i; } } else { for ($i = $current_page + 1; $i <= $current_page + $offset; $i++) { $db_multipage['link'][] = $i; } } $db_multipage['start_row'] = $start_row; $db_multipage['total_row'] = $total_row; $db_multipage['total_page'] = $total_page; $db_multipage['current_page'] = $current_page; $db_multipage['limit'] = $limit; $db_multipage['up_page'] = $up_page; $db_multipage['next_page'] = $next_page; return $db_multipage; } function db_multinum($sql) { global $db; $sql = eregi_replace("select .* from", "SELECT COUNT(*) AS num FROM", $sql); $sql = eregi_replace("limit [ 0-9]+[, ]*[0-9]*", " ", $sql); $query = $db->query($sql); $result = $db->fetchRow(0); return $result['num']; } ?>